#include <gtest/gtest.h>
#include <iostream>
#include "../mqserver/queue.hpp"

using namespace std;
using namespace ns_rabbitmq;
ns_rabbitmq::QueueManager::QueueManagerPtr mqmptr;
class QueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        std::cout << "初始化工作" << std::endl;
        mqmptr = std::make_shared<ns_rabbitmq::QueueManager>("./data/meta.db");
    }

    virtual void TearDown() override
    {
        // mqmptr->clear();
        cout << "清理工作" << endl;
    }
};

TEST(queue_test, test_insert)
{
    std::unordered_map<std::string, std::string> map{{"k1", "v1"}};
    // mqmptr->DeclareQueue("machine1", true, true, true, map);
    // mqmptr->DeclareQueue("machine2", true, true, false, map);
    // mqmptr->DeclareQueue("machine3", true, true, false, map);
    // mqmptr->DeclareQueue("machine4", true, true, false, map);
    EXPECT_EQ(mqmptr->Size(), 3);
}

TEST(queue_test, test_select)
{
    MsgQueuePtr exp = mqmptr->SelectQueue("machine2");
    ASSERT_EQ(exp->_name, "machine2");
    ASSERT_EQ(exp->_exclusive, true);
    ASSERT_EQ(exp->_durable, true);
    ASSERT_EQ(exp->_auto_delete, true);
    ASSERT_EQ(exp->GetArgs(), "k1=v1");
}

TEST(queue_test, test_delete)
{
    mqmptr->DeleteQueue("machine1");
    MsgQueuePtr exp = mqmptr->SelectQueue("machine1");
    ASSERT_EQ(mqmptr->IsExistsQueue("machine1"), false);
    ASSERT_EQ(exp.get(), nullptr);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new QueueTest());
    RUN_ALL_TESTS();
    return 0;
}